package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.UserCase;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.navigator.RestServiceDetail;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.github.aloxc.plugin.restplus.utils.JsonUtils;
import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.openapi.util.IconLoader;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * 保存请求用例
 */
public class SaveCaseListener extends MouseAdapter {
    private RestServiceDetail restServiceDetail;

    public SaveCaseListener(RestServiceDetail restServiceDetail){

        this.restServiceDetail = restServiceDetail;
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if (StringUtils.isNullOrEmpty(restServiceDetail.getUrl())) {
            return;
        }
        super.mouseClicked(e);
        int x = e.getX();
        int y = e.getY();
        JPopupMenu menu = new JPopupMenu();        //创建保存菜单
        JMenuItem save = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.left.save.save.text"));//保存
        save.setIcon(IconLoader.getIcon("/icons/save.png"));
        JMenuItem saveAs = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.left.save.saveas.text"));//另存为
        saveAs.setIcon(IconLoader.getIcon("/icons/saveas.png"));
        menu.add(save);
        menu.add(saveAs);
        save.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        saveAs.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        save.addActionListener(evt -> {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) restServiceDetail.getUserCaseTree().getLastSelectedPathComponent();// 获得选中的欲删除节点
            if (node == null) {
                restServiceDetail.getSaveAsActionListener().actionPerformed(evt);
                return;
            }
            String name = (String) node.getUserObject();
            RestConfig config = restServiceDetail.getProject().getComponent(RestServicesNavigator.class).getConfig();
            Map<String, UserCase> userCaseMap = config.getUserCaseMap().get(
                    (restServiceDetail.getParentName() != null ? restServiceDetail.getParentName() + ":" : "")
                            + restServiceDetail.getUrl());
            UserCase userCase = userCaseMap.get(UserCase.getUserCaseKey(name,restServiceDetail.getMethod()));
            Map<String, Object> headerMap = JsonUtils.toBean(restServiceDetail.getHeaderTextArea().getText(), HashMap.class);
            Map<String, Object> paramMap = JsonUtils.toBean(restServiceDetail.getRequestParamsTextArea().getText(), HashMap.class);
            Map<String, Object> bodyMap = null;
            if (!StringUtils.isNullOrEmpty(restServiceDetail.getRequestBodyTextArea().getText())) {
                bodyMap = JsonUtils.toBean(restServiceDetail.getRequestBodyTextArea().getText(), HashMap.class);
            }
            userCase.setTest(restServiceDetail.getRequestTestTextArea().getText());
            userCase.setHeaderMap(headerMap);
            userCase.setParamMap(paramMap);
            userCase.setBodyMap(bodyMap);
            config.save(restServiceDetail.getProject());
        });
        saveAs.addActionListener(restServiceDetail.getSaveAsActionListener());
        menu.show(restServiceDetail.getSaveCaseButton(), x, y);
    }
}
